home *** CD-ROM | disk | FTP | other *** search
/ PC Pro 2005 December / DPPCPRO1205.ISO / Essentials / Programming / Basic4GL / Setup Basic4GL v2.3.1.exe / $INSTDIR / Programs / Flyaround.gb < prev    next >
Encoding:
Text File  |  2005-07-29  |  3.0 KB  |  90 lines

  1. ' Constant settings
  2. const gridxsize = 20, gridysize = 20
  3. const spacing# = 20
  4. const turnSpeed# = 1, accell# = 0.01
  5.  
  6. ' Data types
  7. struc SPlayer
  8.     dim pos#(3)(3)          ' Current position and direction
  9.     dim speed#              ' Speee
  10. endstruc
  11.  
  12. ' Terrain heightmap
  13. dim h#(gridxsize)(gridysize)
  14.  
  15. ' Player position
  16. dim SPlayer player
  17.  
  18. ' Other info
  19. dim light#(3): light# = Normalize (vec4 (0, 20, 0, 1))
  20.  
  21. ' Working variables
  22. dim x, y, turnx#, turny#, temp#(4)(3), normal#(3), i#, t#(3)
  23.  
  24. ' Make a terrain
  25. for y = 0 to gridysize
  26.     for x = 0 to gridxsize
  27.         h#(x)(y) = rnd() % 10 - 5
  28.     next
  29. next
  30.                    
  31. ' Setup player
  32. player.pos# = MatrixRotateX (-45)
  33. player.pos# (3) = vec4 (gridXSize * spacing# / 2, 200, gridYSize * spacing# / 2, 1)
  34.  
  35. ' Setup OpenGL
  36.     glEnable(GL_LIGHT1)                                 ' Enable Light One
  37.  
  38. ' Main loop
  39. while true
  40.     gosub Render
  41.     updatejoystick()
  42.     while SyncTimer (10): gosub Update: wend
  43. wend
  44.     
  45. end
  46.  
  47. Render:
  48.     ' Clear screen
  49.     glClear (GL_DEPTH_BUFFER_BIT or GL_COLOR_BUFFER_BIT)
  50.     
  51.     ' Setup camera position
  52.     glLoadMatrixf (RTInvert (player.pos#))
  53.     
  54.     ' Draw terrain map
  55.     for y = 0 to gridYSize - 1
  56.         for x = 0 to gridXSize - 1
  57.             temp#(1) = vec4 (x * spacing#,               h#(x)(y),           y * spacing#,              1)
  58.             temp#(2) = vec4 (x * spacing# + spacing#,    h#(x + 1)(y),       y * spacing#,              1)
  59.             temp#(3) = vec4 (x * spacing# + spacing#,    h#(x + 1)(y + 1),   y * spacing# + spacing#,   1)
  60.             temp#(4) = vec4 (x * spacing#,               h#(x)(y + 1),       y * spacing# + spacing#,   1)
  61.             normal# = -Normalize (CrossProduct (temp#(2) - temp#(1), temp#(4) - temp#(1)))
  62.             i# = light# * normal#
  63.             i# = i# * i# * i# * i#
  64.             if i# < .2 then i# = .2 endif
  65.             glColor3f (i#, i#, i#)            
  66.             glBegin (GL_TRIANGLE_FAN)
  67.                 glVertex3fv (temp#(1)): glVertex3fv (temp#(2)): glVertex3fv (temp#(3)): glVertex3fv (temp#(4))
  68.             glEnd ()
  69.         next
  70.     next
  71.  
  72.     SwapBuffers ()
  73.                 
  74.  
  75.     return
  76.  
  77. Update:
  78.     turnx# = joy_x()*0.00003: turny# = joy_y()*0.00003
  79.     if ScanKeyDown (VK_LEFT)    then turnx# = turnx# - turnSpeed# endif
  80.     if ScanKeyDown (VK_RIGHT)   then turnx# = turnx# + turnSpeed# endif
  81.     if ScanKeyDown (VK_UP)      then turny# = turny# - turnSpeed# endif
  82.     if ScanKeyDown (VK_DOWN)    then turny# = turny# + turnSpeed# endif
  83.     if ScanKeyDown (Asc ("A"))  then player.speed# = player.speed# + accell# endif   
  84.     if ScanKeyDown (Asc ("Z"))  then player.speed# = player.speed# - accell# endif
  85.     player.pos# = player.pos# * MatrixRotateX (turny#) * MatrixRotateZ (-turnx#)
  86.     t# = player.pos# (3)
  87.     player.pos# = MatrixRotateY (player.pos# (0)(1) * .5) * player.pos#
  88.     player.pos# (3) = t#
  89.     player.pos# (3) = player.pos# (3) - player.pos#(2) * player.speed#
  90.     return